<template>
  <div class="top-head">
    <div class="banxin flex">
      <div class="welcom">欢迎来到小菜严选</div>
      <div class="right flex">
        <ul class="flex">
          <li>
            <img :src="userInfo.headImg" alt="" />
            <span>用户名：</span>
            <span>{{ userInfo.nickName }}</span>
          </li>
          <li>
            <span>我的鸡腿：</span>
            <span>{{ userInfo.coin }}</span>
          </li>
          <li>
            <span>获取鸡腿</span>
          </li>
          <li>
            <span>小菜官网</span>
          </li>
        </ul>
        <div
          class="btn"
          @click="$store.commit('modifyShowLogin', true)"
          v-show="!isLogin"
        >
          登陆
        </div>
        <div
          v-show="isLogin"
          class="btn"
          @click="$router.push('/personal/cart')"
        >
          <img src="../../assets/images/Shopping.png" alt="" />
          <span>购物车</span>
          <span class="total">{{ cartTotal }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { getUserInfoApi, WXLoginApi } from "../../http/api";
import headImg from "../../assets/images/userImg.png";
import { toast } from "../../utils/toast";
export default {
  data() {
    return {
      userInfo: {
        headImg,
        nickName: "--",
        coin: "--",
      },
      cartTotal: 0,
      isLogin: false,
    };
  },
  created() {
    this.getUserInfo();
    this.WxLogin();
  },
  methods: {
    WxLogin() {
      // 证明这是扫码登陆后跳转
      const code = this.$route.query.code;
      // console.log(code);
      if (code) {
        // 如果微信扫码后 需要向服务器发请求来得到token
        WXLoginApi(code).then((res) => {
          // console.log(res);
          // 如果code = 400 就是登陆失败可能是扫码过期了
          if (res.code === 400) return alert("微信扫码过期");
          if (res.code === 0) {
            localStorage.setItem("x-auth-token", res["x-auth-token"]);
            toast("微信登陆成功", "success");
            this.getUserInfo();
          }
          // this.$router.pash=$route
          // 把code从路由中清除
          // this.$router.push(this.$route.path);
          if (this.$route.path === "/goodsdetial") {
            this.$router.push(
              this.$route.path + "?id=" + localStorage.getItem("detailId")
            );
          } else {
            this.$router.push(this.$route.path);
          }
        });
      }
    },
    async getUserInfo() {
      const token = localStorage.getItem("x-auth-token");
      if (!token) return;
      this.isLogin = true;
      const res = await getUserInfoApi();
      // console.log(res);
      if (res.code === 0) {
        // console.log(res.code);
        this.userInfo = res.data.userInfo;
        this.cartTotal = res.data.cartTotal;
        this.$store.commit("updataInfo", this.userInfo);
      }
    },
  },
  watch: {
    "$route.query": {
      handler() {
        this.WxLogin();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
.top-head {
  width: 100%;
  height: 40px;
  background: #242b39;
  color: #fff;
  line-height: 40px;
  font-size: 14px;
  .banxin {
    justify-content: space-between;
  }
  .right {
    text-align: center;
    img {
      width: 23px;
      height: 22px;
      border-radius: 50px;
      vertical-align: middle;
      margin-right: 5px;
    }
    ul {
      li {
        cursor: pointer;
        margin-right: 20px;
      }
    }
    .btn {
      cursor: pointer;
      width: 124px;
      height: 40px;
      background: #0a328e;
    }
    .total {
      margin-left: 10px;
      padding: 0 5px;
      border-radius: 50%;
      background-color: red;
    }
  }
}
</style>